/**
  *@File文件:UserController.java
  *@Date日期:2017年7月17日 下午3:20:17
  *@Copyright版权:Corporation 2017
  */
package com.qiaomengapp.controller;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qiaomengapp.bean.User;
import com.qiaomengapp.exception.CustomException;
import com.qiaomengapp.service.IUserService;

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private IUserService iUserService;
	
	//用户登录
	@RequestMapping(value="login",produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String login(String phone,String password,HttpSession session){
		try {
			User user = iUserService.select_login(phone, password);
			session.setAttribute("user", user);
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			return e.getMessage();
		}
		return null;
	}
	//用户注册
	@RequestMapping(value="register",produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String register(String password,HttpSession session){
		try {
			String phone=(String) session.getAttribute("phone");
			iUserService.insert_register(phone, password);
			User user = iUserService.select_login(phone, password);
			session.setAttribute("user", user);
			session.removeAttribute("phone");
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			return e.getMessage();
		}
		return null;
	}
	//忘记密码用户的设置新密码
	@RequestMapping(value="changePassword2",produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String changePassword2(String password,HttpSession session){
		String phone=(String) session.getAttribute("phone");
		try {
			iUserService.update_password2(phone, password);
			User user = iUserService.select_login(phone, password);
			session.setAttribute("user", user);
			session.removeAttribute("phone");
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			return e.getMessage();
		}
		return null;
	}
	
	//用户个人中心的信息
	@RequestMapping("selfcenterSetting")
	@ResponseBody
	public User selfcenterSetting(HttpSession session){
		User user=(User) session.getAttribute("user");
		return user;
	}
	//注册的手机号验证
	@RequestMapping(value="validatePhone",produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String validatePhone(String phone){
		try {
			iUserService.select_validatePhone(phone);
		} catch (CustomException e) {
			return e.getMessage();
		}
		return null;		
	}
	
	//找回密码的手机号验证
	@RequestMapping(value="validatePhone2",produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String validatePhone2(String phone){
		try {
			iUserService.select_validatePhone2(phone);
		} catch (CustomException e) {
			return e.getMessage();
		}
		return null;		
	}
	
	//验证验证码
	@RequestMapping(value="validateAuthCode",produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String validateAuthCode(HttpSession session, String authCode,String phone){
		//自动生成的验证码
		String verCode=(String) session.getAttribute("verCode");
		session.setAttribute("phone", phone);
		if(verCode.equalsIgnoreCase(authCode)){
			return null;
		}else{
			return "验证码错误";
		}	
	}
}
